package cn.mw.cmdb.entity;

import cn.mw.cmdb.param.ImportContextParam;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.microMonitorCommon.entity.cmdb.ModelExportDataInfoParam;
import cn.mw.microMonitorCommon.entity.cmdb.MwImportResultParam;
import com.google.common.base.Strings;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PropertyTypeIP extends PropertyType<PropertyTypeIP, String> {

    public static final String ID = "ip";
    private static List<String> finalList = new ArrayList<>();

    private static final String IPPattern = "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";

    public PropertyTypeIP() {
        setId(ID);
        setName("IP");
        setValueTip("请输入IP");
        setSortNum(7);
    }

    @Override
    public String convertValue(Object value) {
        if (value == null) {
            return null;
        }
        return value.toString();
    }

    @Override
    public Criteria doCriteria(CriteriaOpsType opsType, String name, Object value) {
        Criteria criteria = null;
        switch (opsType) {
            case is:
                criteria = new Criteria(name).is(value.toString());
                break;
            case regex:
                criteria = new Criteria(name).regex(value.toString());
                break;
            case in:
                if (value instanceof List) {
                    Object[] objects = ((List) value).toArray();
                    criteria = new Criteria(name).in(objects);
                } else {
                    criteria = new Criteria(name).in(value);
                }
                break;
            case like:
                Pattern pattern = Pattern.compile(regexCharacters(value), Pattern.CASE_INSENSITIVE);
                criteria = new Criteria(name).regex(pattern);
                break;
            case notLike:
                Pattern pattern1 = Pattern.compile(regexCharacters(value), Pattern.CASE_INSENSITIVE);
                criteria = new Criteria(name).not().regex(pattern1);
                break;
            case exist:
                criteria = new Criteria(name).exists((boolean) value);
                break;
            case ne:
                criteria = new Criteria(name).ne(value);
        }
        return criteria;
    }

    @Override
    public boolean validateValue(Object value, PropertyTypeRef propertyTypeRef) throws Exception {
        Map<String, Object> context = propertyTypeRef.getContext();
        if (null != context && null != value) {
            String str = value.toString();
            if (StringUtils.hasText(str)) {
                Pattern pattern = Pattern.compile(IPPattern);
                Matcher matcher = pattern.matcher(str);
                if (!matcher.matches()) {
                    return false;
                }
            }
        }

        return super.validateValue(value, propertyTypeRef);
    }

    @Override
    public CriteriaOpsType getDefaultCriteriaOpsType() {
        return CriteriaOpsType.like;
    }

    @Override
    public PropertyTypeIP newObj() {
        return new PropertyTypeIP();
    }

    @Override
    public void refresh() {
        finalList.clear();
    }

    @Override
    public Object getDataValue(ImportContextParam contextParam) {
        MwImportResultParam resultParam = new MwImportResultParam();
        String message = "";
        ModelExportDataInfoParam importParam = contextParam.getParam();
        //属性名称
        String tableName = importParam.getTableName();
        Object cellValue = importParam.getCellValue();
        Map<String, Object> matchMap = new HashMap<>();
        importCheckInfo(resultParam, contextParam, matchMap);
        boolean isDefaultFlag = false;
        //数值类型为空字符串，默认为0
        if (Strings.isNullOrEmpty(strValueConvert(cellValue).trim())) {
            //空字符串
            if (matchMap != null && matchMap.get(ValueKey) != null) {
                Object obj = matchMap.get(ValueKey);
                isDefaultFlag = true;
                cellValue = obj;
            }
        }
        //是否默认值提示
        String prefixStr = prefixStrKey;
        if (isDefaultFlag) {
            prefixStr = defaultPrefixStrKey;
        }
        String ip = strValueConvert(cellValue);
        if (StringUtils.hasText(ip)) {
            Pattern pattern = Pattern.compile(IPPattern);
            Matcher matcher = pattern.matcher(ip);
            if (!matcher.matches()) {
                message += tableName + prefixStr + ip + "]格式不正确";
                resultParam.setMessage(message);
                return resultParam;
            }
        }
        resultParam.setCellValue(ip);
        return resultParam;
    }
}
